package cn.calm.user.action;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.osgi.interceptor.ServiceAware;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import cn.calm.application.service.IApplicationService;
import cn.calm.commons.action.CurdAction;
import cn.calm.commons.entity.PageMode;
import cn.calm.entity.Application;
import cn.calm.entity.User;
import cn.calm.entity.UserMenuApplication;
import cn.calm.user.service.IMenuService;
import cn.calm.user.service.IUserMenuApplicationService;
import cn.calm.user.service.IUserService;

@SuppressWarnings("rawtypes")
public class UserMenuApplicationAction<T extends UserMenuApplication,S extends IUserMenuApplicationService<T>> extends
		CurdAction<T,S> implements ServiceAware<IUserMenuApplicationService>{
	private static final Logger logger = Logger
			.getLogger(UserMenuApplicationAction.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IUserMenuApplicationService<T> userMenuApplicationService;
	private IApplicationService<Application> applicationService;
	private List<Application> applications;

	/**
	 * 查询桌面的菜单
	 * 
	 * @return
	 */
	public String loadMenuApp() {
		logger.debug("loadMenuApp start");
		User user = (User) getSessionValue(UserAction.SESSION_KEY_USER);
		list = userMenuApplicationService
				.listByUserIdAndParent(user.getId(), t);
		if (t.getId().getMenuId() != null) {
			t.getId().setUserId(user.getId());
			super.load();
		}

		logger.debug("loadMenuApp end");
		return "loadMenuApp";
	}

	public String listAllMenu() {
		logger.debug("listAllMenu start");
		User user = (User) getSessionValue(UserAction.SESSION_KEY_USER);
		list = userMenuApplicationService.listAllMenuByUserId(user.getId());
		logger.debug("loadMenuApp end");
		return "listAllMenu";
	}

	private void listApplication() {
		logger.debug("listApplication start");
		Application app = new Application();
		applications = applicationService.list(app);
		logger.debug("listApplication end");
	}

	@Override
	public String load() {
		try {
			logger.debug("load start");
			listAllMenu();
			listApplication();
			User user = (User) getSessionValue(UserAction.SESSION_KEY_USER);
			t.getId().setUserId(user.getId());
			return super.load();
		} finally {
			logger.debug("load end");
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.desk.commons.action.CurdAction#listPage()
	 */
	@Override
	public String listPage() {
		logger.debug("listPage start");
		if ("".equals(t.getParentId())) {
			t.setParentId(null);
		}
		pageMode = userMenuApplicationService.listPage(pageMode, t);
		logger.debug("listPage end");
		return "listPage";
	}

	@Override
	public String getModeName() {
		return "userMenuApplication";
	}

	public void setUserMenuApplicationService(
			IUserMenuApplicationService<T> userMenuApplicationService) {
		this.userMenuApplicationService = userMenuApplicationService;
	}

	@Override
	public List<T> getList() {
		return super.getList();
	}

	@Override
	public Map<String, List<String>> getFieldErrors() {
		return super.getFieldErrors();
	}

	@Override
	public Collection<String> getActionErrors() {
		return super.getActionErrors();
	}

	@Override
	public T getT() {
		return super.getT();
	}

	@Override
	public PageMode getPageMode() {
		return super.getPageMode();
	}

	public void setApplicationService(
			IApplicationService<Application> applicationService) {
		this.applicationService = applicationService;
	}

	public List<Application> getApplications() {
		return applications;
	}

	public void setApplications(List<Application> applications) {
		this.applications = applications;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Class<T> getEntityClass() {
		return (Class<T>) UserMenuApplication.class;
	}
	@SuppressWarnings("unchecked")
	@Override
	public void setBundleContext(BundleContext bundleContext) {
		super.setBundleContext(bundleContext);
		ServiceReference<IUserMenuApplicationService> serviceRef=bundleContext.getServiceReference(IUserMenuApplicationService.class);
		userMenuApplicationService=bundleContext.getService(serviceRef);
		ServiceReference<IMenuService> menuRef=bundleContext.getServiceReference(IMenuService.class);
		
		userMenuApplicationService.setMenuService(bundleContext.getService(menuRef));
		
		ServiceReference<IUserService> userRef=bundleContext.getServiceReference(IUserService.class);
		userMenuApplicationService.setUserService(bundleContext.getService(userRef));
		ServiceReference<IApplicationService> serviceRefApp=bundleContext.getServiceReference(IApplicationService.class);
		applicationService=bundleContext.getService(serviceRefApp);
		userMenuApplicationService.setApplicationService(applicationService);
		
		service = (S) userMenuApplicationService;
		
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void setServices(List<IUserMenuApplicationService> arg0) {
		if (userMenuApplicationService == null) {
			if (arg0 != null && arg0.size() > 0) {
				userMenuApplicationService = (S) arg0.get(0);
				
			}
		}
	}
}
